package com.zxl.common;

import com.zxl.common.constant.ResponseStatusCode;
import io.swagger.annotations.ApiModelProperty;


/**
 * 返回统一格式
 */
public class Result<T> {

    @ApiModelProperty("是否成功")
    private boolean success;

    @ApiModelProperty("返回状态码")
    private Integer code;

    @ApiModelProperty("状态码描述")
    private String message;

    @ApiModelProperty("返回对象")
    private T data;


    /**
     * 传递单个结果，如果结果不为空，返回成功
     * @param data
     */
    public Result(T data){
        this.success = true;
        this.data = data;
        this.code = ResponseStatusCode.SUCCESS.getCode();
        this.message = ResponseStatusCode.SUCCESS.getMessage();
    }

    /**
     * 传递单个结果，如果结果不为空，返回成功
     * @param responseStatusCode
     */
    public Result(ResponseStatusCode responseStatusCode) {
        if(ResponseStatusCode.SUCCESS.getCode().equals(responseStatusCode.getCode())){
            this.success = true;
        }else{
            this.success = false;
        }
        this.code = responseStatusCode.getCode();
        this.message = responseStatusCode.getMessage();
    }

    public Result() {
    }

    /**
     * 错误构造方法
     * @param code
     * @param errorMsg
     */
    public Result(Integer code, String errorMsg) {
        this.code = code;
        this.message = errorMsg;
        if(ResponseStatusCode.SUCCESS.getCode().equals(code)){
            this.success = true;
        }
        if(ResponseStatusCode.ERROR.getCode().equals(code)){
            this.success=false;
        }
    }

    public Result(Integer code, String msg, boolean isSuccess) {
        this.code = code;
        this.message = msg;
        this.success = isSuccess;
    }

    /**
     * 特殊错误方法
     * */
    public Result(Integer code, String msg, Object data) {
        this.code = code;
        this.message = msg;
        this.data = (T) data;
        this.success = false;
    }

    /**
     * 自定义构造方法
     * @param code
     * @param message
     * @param data
     */
    public Result(T data, Integer code, String message, boolean isSuccess) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.success = isSuccess;
    }

    public void setCodeAndMessage(ResponseStatusCode responseStatusCode){
        if(responseStatusCode==null){
            return;
        }
        this.code = responseStatusCode.getCode();
        this.message = responseStatusCode.getMessage();
    }


    public boolean isSuccess(){
        return success;
    }

    public static Result success(Object data){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.SUCCESS.getCode());
        res.setMessage(ResponseStatusCode.SUCCESS.getMessage());
        res.setSuccess(true);
        res.setData(data);
        return res;
    }

    public static Result success(Object data,String msg){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.SUCCESS.getCode());
        res.setMessage(msg);
        res.setSuccess(true);
        res.setData(data);
        return res;
    }

    public static Result success(){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.SUCCESS.getCode());
        res.setMessage(ResponseStatusCode.SUCCESS.getMessage());
        res.setSuccess(true);
        res.setData(null);
        return res;
    }

    public static Result success(String msg){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.SUCCESS.getCode());
        res.setMessage(msg);
        res.setSuccess(true);
        res.setData(null);
        return res;
    }

    public static Result fail(Object data, String msg){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.ERROR.getCode());
        res.setMessage(msg);
        res.setSuccess(false);
        res.setData(data);
        return res;
    }

    public static Result fail(String msg){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.ERROR.getCode());
        res.setMessage(msg);
        res.setSuccess(false);
        res.setData(null);
        return res;
    }

    public static Result fail(Object data){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.ERROR.getCode());
        res.setMessage(ResponseStatusCode.ERROR.getMessage());
        res.setSuccess(false);
        res.setData(data);
        return res;
    }

    public static Result fail(){
        Result res = new Result<>();
        res.setCode(ResponseStatusCode.ERROR.getCode());
        res.setMessage(ResponseStatusCode.ERROR.getMessage());
        res.setSuccess(false);
        res.setData(null);
        return res;
    }

    public static Result fail(Integer errorCode, String message){
        Result res = new Result<>();
        res.setCode(errorCode);
        res.setMessage(message);
        res.setSuccess(false);
        res.setData(null);
        return res;
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", data=" + data +
                ", code=" + code +
                ", errorMsg='" + message + '\'' +
                '}';
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
